package com.cms.biz.rsf;

import java.sql.Connection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.cms.bean.RSFBean;
import com.cms.config.CategoryTypeDef;
import com.cms.config.ConfigDef;
import com.cms.database.DBHelper;
import com.cms.facade.RequestFacade;
import com.cms.facade.SessionFacade;
import com.cms.framework.Command;
import com.cms.framework.CommandException;
import com.cms.framework.CommandToken;
import com.cms.util.FileHelper;
import com.cms.util.StringHelper;

public class UpdateRSFCommand implements Command {
	private String next;
	public UpdateRSFCommand(String next) {
		this.next = next;
	}

	@SuppressWarnings("finally")
	@Override
	public String execute(HttpServletRequest req) throws CommandException {
        HttpSession session = req.getSession();
		Connection conn = DBHelper.getConn();
		
		if (!CommandToken.isValid(req)) {
			RequestFacade.setSuccessFlag(req, Boolean.FALSE);
			RequestFacade.setInvalidTokenMsg(req);
			return next;
		}
		
		int rsfid = StringHelper.isRequestParamEmptyInt(req, "rsfId", 0);
		int category = StringHelper.isRequestParamEmptyInt(req, "category", CategoryTypeDef.NEW);
		int subcategory = StringHelper.isRequestParamEmptyInt(req, "subcategory", -1);
		int isimg = StringHelper.isRequestParamEmptyInt(req, "isimg", 0);
		int specialId = StringHelper.isRequestParamEmptyInt(req, "specialId", 0);
		int istop = StringHelper.isRequestParamEmptyInt(req, "istop", 0);
		int folderId = StringHelper.isRequestParamEmptyInt(req, "folderId", ConfigDef.ACTIVE);
		int ispassd = StringHelper.isRequestParamEmptyInt(req, "ispassd", ConfigDef.NOT_AUDIT);
		String title = StringHelper.isEmpty(req.getParameter("title"), "");
		String titleColor = StringHelper.isEmpty(req.getParameter("titleColor"), "");
		String author = StringHelper.isEmpty(req.getParameter("author"), "");
		String source = StringHelper.isEmpty(req.getParameter("source"), "");
		String keywords = StringHelper.isEmpty(req.getParameter("keywords"), "");
		String imgName = StringHelper.isEmpty(req.getParameter("imgName"), "");
		String content = StringHelper.isEmpty(req.getParameter("content"), "");
        String userId = SessionFacade.getUser(session);
        
		try {
			RSFBean rsfBean = new RSFBean();
			rsfBean.setId(rsfid);
			rsfBean.setFolderId(folderId);
			rsfBean.setCategoryId(category);
			rsfBean.setSpecialId(specialId);
			rsfBean.setTitle(title);
			rsfBean.setTitleColor(titleColor);
			rsfBean.setAuthor(author);
			rsfBean.setSource(source);
			rsfBean.setIsTop(istop);
			rsfBean.setIsImg(isimg);
			rsfBean.setIsPassd(ispassd);
			rsfBean.setImgName(imgName);
			rsfBean.setKeyWord(keywords);
			rsfBean.setcUser(userId);
            rsfBean.setSubCategoryId(subcategory);
			
            String htmlPath = FileHelper.getRSFContentHtmlPath(rsfid);
			FileHelper.writeRSFContentFile(content, htmlPath);
			RSFHelper.updateRSF(conn, rsfBean);

			RequestFacade.setSuccessFlag(req, Boolean.TRUE);
		} catch (Exception e) {
			e.printStackTrace();
			RequestFacade.setSuccessFlag(req, Boolean.FALSE);
			RequestFacade.setErrorMsg(req, e.getMessage());
		} finally {
			DBHelper.closeConn(conn);
			return next;
		}
	}

}
